const app = getApp();
const util = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        topNum: 0,
        gc_id: 3243,
        goods_list: {},
        init_data: {},
        scrollHeight: '',
        ifGetNextClass: true,
        showToNextClass: true
    },
    /**
     * 计算滚动的可用高度
     */
    computeScrollViewHeight() {
        let that = this;
        let query = wx.createSelectorQuery().in(this);
        query.select('.search_header').boundingClientRect();
        query.exec(res => {
            let searchHeight = res[0].height;
            let windowHeight = wx.getSystemInfoSync().windowHeight;
            let scrollHeight = windowHeight - searchHeight;
            this.setData({
                scrollHeight: scrollHeight
            });
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //请求分享数据
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
            data: {},
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == '200') {
                    this.setData({
                        share_default_title: res.data.datas.title,
                        share_default_image_url: res.data.datas.imageUrl
                    })
                }
            }
        })
        util.share();
        var self = this;
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    key: res.data,
                })
            }
        })
        wx.getStorage({
            key: 'goods_class_init',
            success: (res) => {
                if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                    self.getGoodsClassRequest();
                } else {
                    self.GoodsClassShowDatas(res.data);
                    this.getNextGcid(this.data.gc_id)
                }
            },
            fail: () => {
                //获取列表数据
                self.getGoodsClassRequest();
            }
        })
    },
    toPreveClass: function () {
        // console.log('到顶了')
    },
    scrolling: function (e) {
        this.setData({
            scrollConHeight: e.detail.scrollHeight,
            scrollTop: e.detail.scrollTop
        })
    },
    getGoodsClassRequest: function () {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=goods_class',
            success(res) {
                res.cachTime = Date.parse(new Date());
                wx.setStorage({
                    key: 'goods_class_init',
                    data: res
                })
                self.GoodsClassShowDatas(res);
            }
        });
    },
    GoodsClassShowDatas: function (res) {
        var self = this;
        self.setData({
            goods_list: res.data.datas
        });
        if (res.data.datas != null && res.data.datas.class_list.length > 0) {
            var gc_id = res.data.datas.class_list[0].gc_id;
            wx.getStorage({
                key: 'goods_class_child_all_init_' + gc_id,
                success: (res) => {
                    if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                        self.getGoodsClassChildAllInnitRequest(gc_id);
                    } else {
                        self.GoodsClassChildAllInnitShowDatas(res.data);
                    }
                },
                fail: () => {
                    //获取列表数据
                    self.getGoodsClassChildAllInnitRequest(gc_id);
                }
            })
        }
    },
    getGoodsClassChildAllInnitRequest: function (gc_id) {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=goods_class&op=get_child_all&gc_id=' + gc_id,
            success(cres) {
                cres.cachTime = Date.parse(new Date());
                wx.setStorage({
                    key: 'goods_class_child_all_init_' + gc_id,
                    data: cres
                })
                self.GoodsClassChildAllInnitShowDatas(cres);
                self.getNextGcid(gc_id);
            }
        });
    },

    GoodsClassChildAllInnitShowDatas: function (cres) {
        this.setData({
            init_data: cres.data.datas
        })
    },
    get_child_list: function (event) {
        var self = this;
        self.setData({
            topNum: 0
        });
        var class_id = event.currentTarget.dataset.gcid;
        var selected = event.currentTarget.dataset.selected;
        var viewclass = event.currentTarget.dataset.id;
        if (!viewclass && viewclass != 0) {
            if (this.data.curClassIndex == this.data.goods_list.class_list.length - 1) {
                viewclass = 0
            } else {
                viewclass = this.data.curClassIndex + 1
            }    
        }
        if (viewclass <= 4) {
            self.setData({
                viewclass: 'eachClass0'
            });
        } else {
            self.setData({
                viewclass: 'eachClass' + (viewclass - 5)
            });
        }
        if (selected == 1) {
            wx.navigateTo({
                url: '/pages/search/search?cate_id=' + class_id,
            })
        } else {
            self.getNextGcid(class_id);
            wx.getStorage({
                key: 'goods_class_child_all_' + class_id,
                success: (res) => {
                    if (Date.parse(new Date()) - res.data.cachTime > app.globalData.cachTempTime) {
                        self.getGoodsClassChildAllRequest(class_id);
                    } else {
                        self.GoodsClassChildAllShowDatas(class_id, res.data);
                    }
                },
                fail: () => {
                    //获取列表数据
                    self.getGoodsClassChildAllRequest(class_id);
                }
            })

        }
    },
    getNextGcid: function (gc_id) {
        for (let i = 0; i < this.data.goods_list.class_list.length; i++) {
            if (this.data.goods_list.class_list[i].gc_id == gc_id) {
                if (this.data.curClassIndex == this.data.goods_list.class_list.length - 2) {
                    this.setData({
                        curClassIndex: i,
                        nextGcId: this.data.goods_list.class_list[0].gc_id,
                        nextClassName: this.data.goods_list.class_list[0].gc_name
                    })
                } else {
                    this.setData({
                        curClassIndex: i,
                        nextGcId: this.data.goods_list.class_list[i + 1].gc_id,
                        nextClassName: this.data.goods_list.class_list[i + 1].gc_name,
                    })
                }
            }
        }
    },
    getGoodsClassChildAllRequest: function (class_id) {
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=goods_class&op=get_child_all&gc_id=' + class_id,
            success(res) {
                var data = res.data.datas;
                data.cachTime = Date.parse(new Date());
                wx.setStorage({
                    key: 'goods_class_child_all_' + class_id,
                    data: data
                })
                self.GoodsClassChildAllShowDatas(class_id, res.data.datas);
            }
        })
    },
    GoodsClassChildAllShowDatas: function (class_id, data) {
        this.setData({
            gc_id: class_id,
            init_data: data
        });
    },
    touchstartEvent: function (e) {
        let startPointX = e.changedTouches[0].pageX;
        let startPointY = e.changedTouches[0].pageY;
        this.setData({
            startPointX: startPointX,
            startPointY: startPointY
        })
    },
    touchendEvent: function (e) {
        let endPointX = e.changedTouches[0].pageX;
        let endPointY = e.changedTouches[0].pageY;
        let moveX = endPointX - this.data.startPointX;
        let moveY = endPointY - this.data.startPointY;
        if (moveY < 0) {
            if (this.data.scrollConHeight - this.data.scrollHeight - this.data.scrollTop < 20) {
                if (Math.abs(moveY) > 159) {
                    this.get_child_list(e)
                }
            }
        } else if (moveY > 0) {
            if (this.data.scrollTop == 0) {
                if (Math.abs(moveY) > 159) {
                    // this.get_child_list(e)
                }
            }
        }
        this.setData({
            moveX: moveX,
            moveY: moveY
        })
    },
    onReady: function () {
        this.computeScrollViewHeight();
    },
    onShow: function () {
        util.tabCalCartNum();
    },
    wx_scan: function () {
        util.wx_scan();
    },
    onShareAppMessage: function () {
        let share_key = this.data.key ? this.data.key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.share_default_title,
            imageUrl: this.data.share_default_image_url,
            path: share_path
        }
    }
})